package com.wcy.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wcy.entity.User;
import com.wcy.entity.userVo.UserFiltered;
import com.wcy.service.IUserService;
import com.wcy.util.JwtTokenUtil;
import com.wcy.util.PasswordUtil;
import com.wcy.util.vo.AuthRequest;
import com.wcy.util.vo.AuthResponse;
import jakarta.annotation.Resource;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ProblemDetail;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("auth")
public class AuthorizeController {
    @Resource
    private IUserService userService;
    @PostMapping("login")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<AuthResponse> login(@RequestBody AuthRequest authRequest){
        AuthResponse resp = userService.login(authRequest);
        return resp!=null?
                ResponseEntity.ok(resp):
                ResponseEntity
                        .of(ProblemDetail.forStatusAndDetail(HttpStatusCode.valueOf(403),"登录失败"))
                        .build();
    }

    @PostMapping("register")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Void> register(@RequestBody User user){
        System.out.println(user);
        return userService.register(user)?ResponseEntity.ok().build():ResponseEntity.badRequest().build();
    }

    @PostMapping("logout")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Boolean> logout(String token){
        System.out.println(token);
        return ResponseEntity.ok().build();
    }

    @PostMapping("validate")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Boolean> validate(String token){
        return ResponseEntity.ok(userService.validate(token));
    }
}
